package cn.fangline.wo.task.akka.actors;

import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import cn.fangline.wo.task.akka.messages.AgentHousePage;
import cn.fangline.wo.task.akka.messages.CheckAlive;
import cn.fangline.wo.task.akka.messages.EndPoint;

import javax.xml.ws.Endpoint;

/**
 * Created by 抽离 on 2017/6/26 0026.
 */
public class HouseMasterActor extends AbstractActor {

    private ActorRef houseActor;

    public HouseMasterActor(ActorRef houseActor) {
        this.houseActor = houseActor;
    }

    private boolean isAlive = true;

    @Override
    public void preStart() throws Exception {
        super.preStart();
    }

    @Override
    public Receive createReceive() {
        return
                receiveBuilder()
                        .match(AgentHousePage.class, this::receiveHousePage)
                        .match(CheckAlive.class, this::onReceiveCheckAlive)
                        .match(EndPoint.class, this::onReceiveEndPoint)
                        .build();
    }

    private  void onReceiveEndPoint(EndPoint m) {
        isAlive = true;
    }

    private void onReceiveCheckAlive(CheckAlive m) {
        if(isAlive) {
            getSender().tell(true, getSelf());
            isAlive = false;
        }else{
            getSender().tell(false, getSelf());
        }
    }

    private void receiveHousePage(AgentHousePage agentHousePage) {
        isAlive = true;
        houseActor.tell(agentHousePage, getSelf());
    }
}
